package com.example.healkep.utils;

import androidx.navigation.NavDestination;

import com.example.healkep.utils.Imp.IRespResultCode;
import com.example.healkep.utils.enums.BaseRespResultCode;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;


import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Objects;

public class RespResult<T> implements Serializable {
    private int code = BaseRespResultCode.OK.getCode();
    private String message = BaseRespResultCode.OK.getMessage();
    private String detailMessage = BaseRespResultCode.OK.getDetailMessage();
    private T data;
    private String env;
    private String session;

    public RespResult(int code,String message,String detailMessage){
        this.code = code;
        this.message = message;
        this.detailMessage = detailMessage;
    }
    public RespResult(int code,String message,String detailMessage,T data,String env,String session){
        this.code = code;
        this.message = message;
        this.detailMessage = detailMessage;
        this.data = data;
        this.env = env;
        this.session = session;
    }

    public RespResult(IRespResultCode resultCode, T data, String env, String session){
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.detailMessage = resultCode.getDetailMessage();
        this.data = data;
        this.env = env;
        this.session = session;
    }

    public RespResult(T data,String env,String session){
        this.data = data;
        this.env = env;
        this.session = session;
    }

    @SuppressWarnings("unchecked")
    public RespResult(String json,Type type){
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(json,JsonObject.class);
        Field[] fields = RespResult.class.getDeclaredFields();
        for(Field i : fields){
            if(jsonObject.has(i.getName())){
                if(i.getName().equals("data")){
                    if(type.equals(String.class)){
                        this.data = (T) jsonObject.get(i.getName()).toString();
                    }else{
                        this.data =  gson.fromJson(jsonObject.get(i.getName()), type);
                    }

                }else if(i.getName().equals("code")){
                    this.code = jsonObject.get(i.getName()).getAsInt();
                }else{
                    try {
                        i.setAccessible(true);
                        i.set(this,jsonObject.get(i.getName()).getAsString());
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException("IllegalAccessException:"+i.getName());
                    }
                }
            }else{
                throw new IllegalArgumentException();
            }
        }
    }

    public boolean isOk(){
        return BaseRespResultCode.OK.getCode() == this.code;
    }

    public boolean isNotOK(){
        return !isOk();
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getDetailMessage() {
        return detailMessage;
    }

    public T getData() {
        return data;
    }

    public String getEnv() {
        return env;
    }

    public String getSession() {
        return session;
    }
}
